﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MeizuSDK.Core
{
    /// <summary>
    /// 网络
    /// </summary>
    public static class Networking
    {
        #region DllImport
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern int CalllNotifyApiStartGprsConnect2(IntPtr hMainWnd, uint dwAppType);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern int CalllNotifyApiStopGprsConnect2(IntPtr hMainWnd);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern int CalllNotifyApiQueryNetWorkStatus();

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern uint CallNotifyApiDial_GetMissCallCnt();
        #endregion

        #region 常量
        const int RESULT_ERROR = 0x00000003;
        const int RESULT_OK = 0x00000001;
        #endregion

        /// <summary>
        /// 获取当前网络状态
        /// </summary>
        public static NetworkingStatus Status
        {
            get
            {
                return (NetworkingStatus)CalllNotifyApiQueryNetWorkStatus();
            }
        }
        /// <summary>
        /// 获取未接电话数量
        /// </summary>
        public static uint MissedCallCount
        {
            get{
                return CallNotifyApiDial_GetMissCallCnt();
            }
        }
        /// <summary>
        /// 打开GPRS连接
        /// </summary>
        /// <param name="hMainWnd">打开此网络连接的窗口句柄。此参数已无用，可以设置为IntPtr.Zero</param>
        /// <returns>true表示打开成功；false表示打开失败</returns>
        public static bool GprsConnect(IntPtr hMainWnd)
        {
            return GprsConnect(hMainWnd, GprsAppType.Default);
        }
        /// <summary>
        /// 打开Gprs连接
        /// </summary>
        /// <param name="hMainWnd">打开此网络连接的窗口句柄。此参数已无用，可以设置为IntPtr.Zero</param>
        /// <param name="appType">拨号类型</param>
        /// <returns>true表示打开成功；false表示打开失败</returns>
        public static bool GprsConnect(IntPtr hMainWnd,GprsAppType appType)
        {
            int result = CalllNotifyApiStartGprsConnect2(hMainWnd, (uint)appType);

            return result == RESULT_OK;
        }
        /// <summary>
        /// 打开Gprs连接
        /// </summary>
        /// <param name="appType">拨号类型</param>
        /// <returns>true表示打开成功；false表示打开失败</returns>
        public static bool GprsConnect(GprsAppType appType)
        {
            return GprsConnect(IntPtr.Zero, appType);
        }
        /// <summary>
        /// 打开Gprs连接
        /// </summary>
        /// <returns>true表示打开成功；false表示打开失败</returns>
        public static bool GprsConnect()
        {
            return GprsConnect(GprsAppType.Default);
        }
        /// <summary>
        /// 关闭GPRS连接
        /// </summary>
        /// <param name="hMainWnd">打开此网络连接的窗口句柄。此参数已无用，可以设置为IntPtr.Zero</param>
        /// <returns>true表示关闭成功，false表示关闭失败。</returns>
        public static bool GprsDisconnect(IntPtr hMainWnd)
        {
            return CalllNotifyApiStopGprsConnect2(hMainWnd) == RESULT_OK;
        }
        /// <summary>
        /// 关闭GPRS连接
        /// </summary>
        /// <returns>true表示关闭成功，false表示关闭失败。</returns>
        public static bool GprsDisconnect()
        {
            return GprsDisconnect(IntPtr.Zero);
        }
    }

    /// <summary>
    /// 网络状态
    /// </summary>
    public enum NetworkingStatus : int
    {
        /// <summary>
        /// EDGE网络，无须代理网关. 
        /// </summary>
        Edge = 0x00000005, 
        /// <summary>
        /// 需要设置代理网关的网络，接入点CMWAP，代理网关：10.0.0.172:80 
        /// </summary>
        EdgeProxy = 0x00000004, 
        /// <summary>
        /// 无网络
        /// </summary>
        None = 0x00000001, 
        /// <summary>
        /// USB ActiveSync网络. 
        /// </summary>
        UsbCable = 0x00000003, 
        /// <summary>
        /// WiFi网络. 
        /// </summary>
        Wifi = 0x00000002
    }
    /// <summary>
    /// GPRS拨号类型
    /// </summary>
    public enum GprsAppType
    {
        /// <summary>
        /// 默认拨号模式。按照系统默认规则来拨号，系统的默认规则是在没有网络的的时候，才进行拨号
        /// </summary>
        Default = 0x00000005,
        /// <summary>
        /// 系统程序留用
        /// </summary>
        Email = 0x00000003,
        /// <summary>
        /// 强制拨号。会挂断已连接的GPRS，重新拨号
        /// </summary>
        Force = 0x00000004,
        /// <summary>
        /// 系统程序留用
        /// </summary>
        IE = 0x00000002,
        /// <summary>
        /// 系统程序留用。彩信模式
        /// </summary>
        MMS = 0x00000001
    }
}
